<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends MY_Controller {

    public function __construct() {
        parent::__construct();
        
        if (!isset($this->session->userdata['login']) || $this->session->userdata['login'] == NULL || $this->session->userdata('logged_in') != TRUE) {
            //header('WWW-Authenticate: Basic realm="My Realm"');
            $this->session->sess_destroy();
            header('HTTP/1.0 401 Unauthorized');
            exit();
            //redirect('user/auth'); // the user is not logged in, redirect them!
        } else {
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
            header("Cache-Control: no-store, no-cache, must-revalidate");
            header("Cache-Control: post-check=0, pre-check=0", false);
            header("Pragma: no-cache");
        }
        
        $this->load->model('tree_model');
        $this->load->model('bookmarks_model');
        $this->load->model('setting_model');
        $this->load->model('user_profiles', 'user_profiles_model');
        $this->load->model('search_model');
    }

    public function index() {
        echo '';
    }

    
    /* Tree */
    public function show_node() {
        echo $this->tree_model->get_tree_node($this->uri->segment(3));
    }

    
    /* Bookmarks */
    public function add_bookmark() {
        $res = $this->bookmarks_model->add_bookmark_to_profile($this->uri->segment(3), $this->uri->segment(4,0));
        header('Content-type: application/json');
        echo $this->get_json($res);
    }

    public function remove_bookmark() {
        $res = $this->bookmarks_model->remove_bookmark_from_profile($this->uri->segment(3));
        header('Content-type: application/json');
        echo $this->get_json($res);
    }

    public function share_bookmark() {
        $res = $this->bookmarks_model->share_bookmarks($this->uri->segment(3), $this->uri->segment(4));
        header('Content-type: application/json');
        echo $this->get_json($res);
    }
    
    public function get_bookmarks() {
        echo $this->bookmarks_model->get_tree_bookmarks();
    }

    public function add_dir_bkmk() {
        $res = $this->bookmarks_model->modify_folder($this->uri->segment(3));
        header('Content-type: application/json');
        echo $this->get_json($res);
    }

    public function rename_dir_bkmk() {
        $res = $this->bookmarks_model->modify_folder($this->uri->segment(3), $this->uri->segment(4));
        header('Content-type: application/json');
        echo $this->get_json($res);
    }

    public function change_parent() {
        $res = $this->bookmarks_model->change_parent($this->uri->segment(3), $this->uri->segment(4));
        header('Content-type: application/json');
        echo $this->get_json($res);
    }

    public function copy_bkmk() {
        $res = $this->bookmarks_model->copy_bookmark($this->uri->segment(3), $this->uri->segment(4));
        header('Content-type: application/json');
        echo $this->get_json($res);
    }

    public function change_bkmk() {
        $res = $this->user_profiles_model->change_bookmark($this->uri->segment(3), $this->uri->segment(4));
        header('Content-type: application/json');
        echo $this->get_json($res);
    }

    public function change_node() {
        $res =  $this->user_profiles_model->change_node($this->uri->segment(3), $this->uri->segment(4));
        header('Content-type: application/json');
        echo $this->get_json($res);
    }

    public function set_cookie() {
        $settings = array();
        $chart = array();
        $qstr = replace_quotes($_SERVER['QUERY_STRING']);
        parse_str($qstr, $settings);
        $chart_period = isset($settings['period']) && (0 != count($settings['period'])) ? $settings['period'] : array();
        $chart_mode = isset($settings['chartmode']) && (0 != count($settings['chartmode'])) ? $settings['chartmode'] : array();
        $chart['width'] = $settings['widthchart'];
        $chart['height'] = $settings['heightchart'];
        $chart['reload'] = $settings['reloadchart'];

        if (isset($settings['usepreview'])) {
            $chart['usepreview'] = 'checked';
            $chart['widthpreview'] = $settings['widthpreview'];
            $chart['heightpreview'] = $settings['heightpreview'];
        } else {
            $chart['usepreview'] = '';
            $chart['widthpreview'] = NULL;
            $chart['heightpreview'] = NULL;
        }
        $body_color = (isset($settings['inputColor']))?$settings['inputColor']:'';
        echo $this->setting_model->update_settings($chart, $chart_period, $chart_mode, $body_color);
    }

    public function live_search() {
        header('Content-type: application/json');
        if(isset($_GET['term'])){
            echo $this->search_model->live_search($_GET['term']);
        }else{
            echo '';
        }
    }

    public function full_search() {
        $str = urldecode($this->uri->segment(3));
        echo $this->search_model->show_search_result($str); //, $this->uri->segment(5));
    } //*/
    
    /**!!!
     * 
     * @param bool $result
     * @param string $message
     * @return string JSON string
     */
    private function get_json($result, $message = null) {
        $json['code'] = (boolean) $result ? 200 : 0;
        if (!empty($message)) $json['msg'] = $message;
        return json_encode($json);
    }

}
